!
!    MUNIPHOT    Aperture photometry of image series."
!    Copyright (C) 1997-9  Filip Hroch, Masaryk University, Brno, CZ
!
!
!CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
!C                                                                      C
!C !!!!!!!!!!!!!!!!     This is UNIX version  !!!!!!!!!!!!!!!!!!!!!!!!! C
!C                                                                      C
!CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
!
! This source file contains UNIX specific functions:
!    getarg, iargc, getenv
!
!--------------------------------------------------------------------------
!
!
!  MAIN for Muniphot.
!
! This routine:
!
! * reads the input image names from STDIN, input file or command line
!
! * reads the control commands with awk-like syntax from a command
!    file (after option 'com=' or '-f'). A commands in command line
!    are an alternative (and a new) form of the controling:
!    An equivalent form:
!    
!    file              at command line
!    at                '{ at; sky; find ...}'
!    sky
!    ! comment
!    find ...
!   
!and it pass the values to MUNIPHOT.FOR.
!

program main_for_muniphot

  implicit none
  
  integer, parameter :: LEN = 80

  character(len=LEN) :: par,line,com=".",namdir=" "
  integer :: i, j, icit = 1
  logical :: exist, stdinput = .false.
  integer :: iargc

  write(*,*) "MUNIPHOT Version 2.9, Copyright (C) 1998-9 F.Hroch, Masaryk University, Brno,CZ"
  write(*,*) "Muniphot comes with ABSOLUTELY NO WARRANTY; This is free software, and you are"
  write(*,*) "welcome to redistribute under certain conditions; see 'gnulic.doc' for details."
  write(*,*)

  do i = 1, iargc()

     call getarg(i,par);

     ! flag for input from stdin, dirfile
     if( par(1:1) == "@" ) then
        namdir = par(2:)
        stdinput = namdir == " "
     end if

     if( par(1:4) == "com=" )then
        com = par(5:)
        inquire(file=com,exist=exist,err=666)
        if( .not. exist ) write(*,*) "A command file not found."
     end if

     if( par == "-f" )then
        j = i + 1
        call getarg(i+1,com)
        inquire(file=com,exist=exist,err=666)
        if( .not. exist ) write(*,*) "A command file not found."
     end if

  end do

! default command file  
  if( com == '.' )then                         ! firstly, read users default
     call getenv("HOME",com)
     com = trim(com)//'/.muniphot.com'
     inquire(file=com,exist=exist,err=666)
     if( .not. exist )then                      ! not exist => create it
        com = 'muniphot.com'
        open(11,file=com,status='new',iostat=i)
        if( i == 0 )then
           write(11,*,err=666) 'attach'
           write(11,*,err=666) 'find 1 1'
           write(11,*,err=666) 'photometry'
           write(11,*,err=666) 'sort AP n +4'
           close(11,err=666)
        endif
     endif
  endif

  ! image name from file
  if( namdir /= " " ) then
     open(11,file=namdir,status='old',iostat=i)
     if( i /= 0 ) then
        write(*,*) "Can't open file: ",namdir
        stop
     endif
     do
        icit = icit + 1
        read(11,"(A)",end=20) line
        call muniphot(line,com)  
     enddo
20   close(11)
  endif

  ! image name by command line
  do i = 1, iargc()
     call getarg(i,par)
     if( index(par,"=") == 0 .and. par(1:1) /= "@" .and.  &
          par(1:1) /= "-" .and. j /= i ) then
        line = par
        call muniphot(line,com)  
        icit = icit + 1
     endif
  enddo

  ! images name by standart input
  if( stdinput ) then
     do
        icit = icit + 1
        read(*,"(A)",end=30) line
        call muniphot(line,com)  
     enddo
30   continue
  endif

  if( iargc() < 1 .and. icit == 1) then
     write(*,*) "MUNIPHOT   Aperture photometry of image series."
     write(*,*) "This program contained procedures SKY, FIND, FOTOMETER and SORT from"  
     write(*,*) "the DAOPHOT package by P.B.Stetson."
     write(*,*)
     write(*,*) "Usage: muniphot [@dirfile] [com=commandfile] [image1] .."
     write(*,*)
     write(*,*) "@dirfile= file with name(s) of the scientific exposures, no wildcards *,?"
     write(*,*) "           (for only '@', read image(s) name(s) from standart input)"
     write(*,*) "commandfile= file with commands (see Example)"
     write(*,*) "image1...= name(s) of simple scientific exposures(s)"
     write(*,*) "Example of command file:"
     write(*,*) "ATTACH"
     write(*,*) "FIND 1,1"
     write(*,*) "PHOTOMETRY"
     write(*,*) "SORT AP N +4"
     write(*,*) "(The $HOME/.muniphot.com read or ./muniphot.com create for com=. or '-f .')" 
  endif

  stop

! ---

666 write(*,*) 'Unspecified I/O error.'

end

!----------------------------------------------------------------------

! Dummy routine. 
!
!
!subroutine muniphot ( line, com)
!
!implicit none
!
!character(len=*) line,com
!
!write(*,*) line
!write(*,*) com
!
!end subroutine muniphot
